

package org.koyabr.webx.demo.danmaku.web.common;

import static com.alibaba.citrus.util.Assert.*;
import static com.alibaba.citrus.util.ObjectUtil.*;
import static com.alibaba.citrus.util.StringUtil.*;

import java.io.Serializable;

public class DanmakuUser implements Serializable {
    private static final long                      serialVersionUID = -7507510429755782596L;
    private static final ThreadLocal<DanmakuUser> userHolder       = new ThreadLocal<DanmakuUser>();
    private String   username;
    
    public static final DanmakuUser getCurrentUser() {
        return userHolder.get();
    }

    public static final void setCurrentUser(DanmakuUser user) {
        userHolder.set(user);
    }

    /** 创建匿名用户。 */
    public DanmakuUser() {
    }

    /** 创建用户。 */
    public DanmakuUser(String username) {
        this.username = trimToNull(username);
    }

    public String getUsername() {
        return username;
    }

    public void upgrade(String username) {
        assertTrue(!hasLoggedIn(), ExceptionType.ILLEGAL_STATE, "already logged in");

        username = assertNotNull(trimToNull(username), "no username");

        this.username = username;
    }

    public boolean hasLoggedIn() {
        return username != null;
    }

    @Override
    public String toString() {
        return "DanmakuUser[" + defaultIfNull(username, "anonymous") + "]";
    }
}
